﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;
using Core;

namespace TestProject
{
    public partial class NewSpecie : Form
    {
        private Model model;
        private GeneTable gen_t = null;
        private ChromosomeTable chrom_t = null;
        private ChromosomeGeneLink chrom_link = null;
        private CrossingoverLink cross_link = null;
        private PhenotypeTable phen_t = null;
        private GenomePhenotypeLink genph_link = null;
        private NaturalSelectionTable natsel_t = null;
        public NewSpecie(Model m)
        {
            InitializeComponent();
            model = m;
        }

        private void NewSpecie_Load(object sender, EventArgs e)
        {
            foreach (GeneTable t in model.model_gene_tables.Values)
            {
                gen_t_box.Items.Add(t.name);
            }
            foreach (ChromosomeTable t in model.model_chromosome_tables.Values)
            {
                chrom_t_box.Items.Add(t.name);
            }
            foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
            {
                phen_t_box.Items.Add(t.name);
            }
        }

        private void chrom_t_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ChromosomeTable t in model.model_chromosome_tables.Values)
            {
                if ((string)chrom_t_box.SelectedItem == t.name)
                {
                    chrom_t = t;
                    break;
                }
            }
            chromgen_l_box.Items.Clear();
            cross_l_box.Items.Clear();
            if (gen_t != null)
            {
                foreach (ChromosomeGeneLink link in model.model_chromosome_gene_links.Values)
                {
                    if ((link.path_to_chromosome_table == chrom_t.path) && (link.path_to_gene_table == gen_t.path))
                        chromgen_l_box.Items.Add(link.name);
                }
            }
            foreach (CrossingoverLink link in model.model_crossingover_links.Values)
            {
                if (link.path_to_chromosome_table == chrom_t.path)
                    cross_l_box.Items.Add(link.name);
            }

        }

        private void phen_t_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
            {
                if ((string)phen_t_box.SelectedItem == t.name)
                {
                    phen_t = t;
                    break;
                }
            }
            genphen_l_box.Items.Clear();
            natsel_t_box.Items.Clear();
            foreach (NaturalSelectionTable t in model.model_natural_selection_tables.Values)
            {
                if (t.path_to_phenotype_table==phen_t.path)
                    natsel_t_box.Items.Add(t.name);                
            }
            if (gen_t != null)
            {
                foreach (GenomePhenotypeLink link in model.model_genome_phenotype_links.Values)
                {
                    if ((link.path_to_phenotype_table == phen_t.path) && (link.path_to_gene_table == gen_t.path))
                        genphen_l_box.Items.Add(link.name);
                }
            }
            genphen_l_box.SelectedItem = null;
            natsel_t_box.SelectedItem = null;
        }

        private void gen_t_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GeneTable t in model.model_gene_tables.Values)
            {
                if ((string)gen_t_box.SelectedItem == t.name)
                {
                    gen_t = t;
                    break;
                }
            }
            chromgen_l_box.Items.Clear();
            genphen_l_box.Items.Clear();
            if (chrom_t != null)
            {
                foreach (ChromosomeGeneLink link in model.model_chromosome_gene_links.Values)
                {
                    if ((link.path_to_chromosome_table == chrom_t.path) && (link.path_to_gene_table == gen_t.path))
                        chromgen_l_box.Items.Add(link.name);
                }
            }
            if (phen_t != null)
            {
                foreach (GenomePhenotypeLink link in model.model_genome_phenotype_links.Values)
                {
                    if ((link.path_to_phenotype_table == phen_t.path) && (link.path_to_gene_table == gen_t.path))
                        genphen_l_box.Items.Add(link.name);
                }
            }
            chromgen_l_box.SelectedItem = null;
            genphen_l_box.SelectedItem = null;
        }

        private void canc_butt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            if ((phen_t != null) && (chrom_link != null) && (gen_t != null) && (chrom_t != null) && (natsel_t != null) && (cross_link != null) && (genph_link != null))
            {
                if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Species\\" + name_box.Text + "\\species.xml"))
                {
                    MessageBox.Show("A specie with this name already exists. Please, choose another name.");
                    return;
                }
                System.IO.Directory.CreateDirectory("file_input\\" + model.model_name + "\\Species\\" + name_box.Text);
                XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Species\\" + name_box.Text + "\\species.xml", null);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("species");
                writer.WriteAttributeString("Name", name_box.Text);
                writer.WriteAttributeString("AmountPopulations", "0");
                writer.WriteStartElement("populations");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
                MyXmlDocument doc = new MyXmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
                XmlElement el;
                el = doc.CreateElement("sp");
                el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Species\\" + name_box.Text + "\\species.xml");
                el.SetAttribute("PathToChromosomeTable", chrom_t.path);
                el.SetAttribute("PathToGeneTable", gen_t.path);
                el.SetAttribute("PathToChromosomeGeneLink", chrom_link.path);
                el.SetAttribute("PathToPhenotypeTable", phen_t.path);
                el.SetAttribute("PathToGenomePhenotypeLink", genph_link.path);
                el.SetAttribute("PathToNaturalSelectionTable", natsel_t.path);
                el.SetAttribute("PathToCrossingoverLink", cross_link.path);
                doc.Increment(doc.GetElementsByTagName("species")[0].Attributes[0]);
                doc.Increment(doc.DocumentElement.Attributes[3]);
                doc.GetElementsByTagName("species")[0].AppendChild(el);
                doc.Save(model.model_path);
                Functions.add_node_func(new Species("file_input\\" + model.model_name + "\\Species\\" + name_box.Text + "\\species.xml"), name_box.Text, "spec");
                this.Close();
            }
            else
            {
                MessageBox.Show("Not all parameters are choosen!");
                return;
            }
        }

        private void natsel_t_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (NaturalSelectionTable t in model.model_natural_selection_tables.Values)
            {
                if ((string)natsel_t_box.SelectedItem == t.name)
                {
                    natsel_t = t;
                    break;
                }
            }
        }

        private void genphen_l_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (GenomePhenotypeLink l in model.model_genome_phenotype_links.Values)
            {
                if ((string)genphen_l_box.SelectedItem == l.name)
                {
                    genph_link = l;
                    break;
                }
            }
        }

        private void chromgen_l_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ChromosomeGeneLink l in model.model_chromosome_gene_links.Values)
            {
                if ((string)chromgen_l_box.SelectedItem == l.name)
                {
                    chrom_link = l;
                    break;
                }
            }
        }

        private void cross_l_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (CrossingoverLink l in model.model_crossingover_links.Values)
            {
                if ((string)cross_l_box.SelectedItem == l.name)
                {
                    cross_link = l;
                    break;
                }
            }
        }
    }
}
